<template>
  <div>
    <van-nav-bar title="影院"  ref="navbar" @click-left="handleLeft" @click-right="handleRight">
        <template #left>
            {{cityName}}<van-icon name="arrow-down" />
      </template>
      <template #right>
        <van-icon name="search" size="28" color="black" />
      </template>
    </van-nav-bar>
    <div
      class="box"
      :style="{
        height: height,
      }"
    >
      <ul>
        <li v-for="data in cinemasList" :key="data.cinemaId">
          <div class="left">
            <div class="cinema_name">{{ data.name }}</div>
            <div class="cineme_text">{{ data.address }}</div>
          </div>

          <div class="right cinema_name">
            <div style="color: red">￥{{ data.lowPrice / 100 }}起</div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import BetterScroll from 'better-scroll'
import { mapState, mapActions, mapMutations } from 'vuex'

export default {
  data () {
    return {
      // cinemaList: [],
      height: '0px'
    }
  },
  computed: {
    ...mapState(['cinemasList', 'cityId', 'cityName'])
  },
  mounted () {
    // console.log(this.$refs.navbar.$el.offsetHeight)
    // 动态计算高度
    this.height =
      document.documentElement.clientHeight -
      this.$refs.navbar.$el.offsetHeight -
      document.querySelector('footer').offsetHeight +
      'px'
      // dispatch 分发
    if (this.cinemasList.length === 0) {
      this.getCinemaData(this.cityId).then(res => {
        this.$nextTick(() => {
          new BetterScroll('.box', {
            scrollbar: {
              fade: true
            }
          })
        })
      })
    } else {
      console.log('走缓存')
      this.$nextTick(() => {
      // 防止初始化过早的问题
        new BetterScroll('.box', {
          scrollbar: {
            fade: true
          }
        })
      })
    }

    // http({
    //   url: `/gateway?cityId=${this.$store.state.cityId}&ticketFlag=1&k=9734767`,
    //   headers: {
    //     'X-Host': 'mall.film-ticket.cinema.list'
    //   }
    // }).then((res) => {
    //   this.cinemaList = res.data.data.cinemas

    // this.$nextTick(() => {
    //   // 防止初始化过早的问题
    //   new BetterScroll('.box', {
    //     scrollbar: {
    //       fade: true
    //     }
    //   })
    // })
    // })
  },
  methods: {
    ...mapActions(['getCinemaData']),
    ...mapMutations(['clearCinema']),
    handleLeft () {
      this.$router.push('/city')

      // 清空cinemaList
      this.clearCinema()
    },
    handleRight () {
      this.$router.push('/cinemas/search')
    }
  }
}
</script>
<style lang="scss" scoped>
li {
  padding: 0.9375rem;
  display: flex;
  justify-content: space-between;
  .left {
    width: 13.25rem;
  }
  .cineme_text {
    color: #797d82;
    font-size: 0.75rem;
    margin-top: 0.3125rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    widows: 12.5rem;
  }
  .cinema_name {
    font-size: 0.9375rem;
  }
}
.box {
  overflow: hidden;
  position: relative; //解决scrollbar 超出范围的问题
}
</style>
